/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.flipkart.ohos.proteus.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.StackLayout;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import com.flipkart.ohos.proteus.DemoApplication;
import com.flipkart.ohos.proteus.LayoutManager;
import com.flipkart.ohos.proteus.ProteusContext;
import com.flipkart.ohos.proteus.ProteusLayoutInflater;
import com.flipkart.ohos.proteus.ProteusView;
import com.flipkart.ohos.proteus.ResourceTable;
import com.flipkart.ohos.proteus.StyleManager;
import com.flipkart.ohos.proteus.Styles;
import com.flipkart.ohos.proteus.api.ProteusManager;
import com.flipkart.ohos.proteus.value.Layout;
import com.flipkart.ohos.proteus.value.ObjectValue;

import java.util.Map;

/**
 * 跳转WebView
 *
 * @since 2021-05-10
 */
public class WebViewAbilitySlice extends AbilitySlice implements ProteusManager.Listener {
    private ProteusLayoutInflater proteusLayoutInflater;
    private ObjectValue data;
    private Layout layout;
    private Styles styles;
    private Map<String, Layout> layouts;
    private ProteusManager proteusManager;

    private StackLayout container;

    private final StyleManager styleManager = new StyleManager() {
        @Override
        protected Styles getStyles() {
            return styles;
        }
    };

    private final LayoutManager layoutManager = new LayoutManager() {
        @Override
        protected Map<String, Layout> getLayouts() {
            return layouts;
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_web_view);
        container = (StackLayout) findComponentById(ResourceTable.Id_create_layout);
        proteusManager = DemoApplication.getProteusManager();
        ProteusContext proteusContext = proteusManager.getProteus().createContextBuilder(WebViewAbilitySlice.this)
            .setLayoutManager(layoutManager)
            .setStyleManager(styleManager)
            .build();
        proteusLayoutInflater = proteusContext.getInflater();
        proteusManager.addListener(this);
        proteusManager.load();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        proteusManager.removeListener(this);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    void render() {
        // remove the current view
        container.removeAllComponents();
        // Inflate a new view using proteus
        ProteusView view = proteusLayoutInflater.inflate("WebViewLayout", data);
        // Add the inflated view to the container
        container.addComponent(view.getAsView());
    }

    void reload() {
        proteusManager.update();
    }

    @Override
    public void onLoad() {
        data = proteusManager.getData();
        layouts = proteusManager.getLayouts();
        styles = proteusManager.getStyles();
        render();
    }

    @Override
    public void onError(String e) {
        new ToastDialog(this)
            .setText(e)
            .setAlignment(LayoutAlignment.CENTER)
            .show();
    }
}
